package com.aerilys.taskboard.client;

import com.aerilys.taskboard.client.activities.AppActivityMapper;
import com.aerilys.taskboard.client.places.AddTaskViewPlace;
import com.aerilys.taskboard.client.places.AppPlaceHistoryMapper;
import com.aerilys.taskboard.client.places.GeneralViewPlace;
import com.aerilys.taskboard.client.tools.ClientFactory;
import com.aerilys.taskboard.shared.FieldVerifier;
import com.aerilys.taskboard.shared.Note;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineHTML;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint
{
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  private static final String SERVER_ERROR = "An error occurred while "
      + "attempting to contact the server. Please check your network "
      + "connection and try again.";

  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private final NoteServiceAsync greetingService = GWT.create(NoteService.class);

  private Place defaultPlace = new GeneralViewPlace("Accueil");

  private SimplePanel appWidget = new SimplePanel();

  private ClientFactory clientFactory;

  /**
   * This is the entry point method.
   */
  public void onModuleLoad()
  {
    clientFactory = GWT.create(ClientFactory.class);

    RootPanel rootPanel = RootPanel.get();

    HTMLPanel header = new HTMLPanel("Aerilys Taskboard");
    header.getElement().setId("header");
    rootPanel.add(header);

    if (isMobile())
    {
      InlineHTML mobileAddButton = new InlineHTML("+");
      mobileAddButton.addClickHandler(new ClickHandler()
      {
        public void onClick(ClickEvent event)
        {
          addNote();
        }
      });
      header.add(mobileAddButton);
    }

    if (!isMobile())
    {
      HTMLPanel menu = new HTMLPanel("");
      menu.getElement().setId("menu");
      rootPanel.add(menu);

      Button addButton = new Button("Ajouter une t\u00E2che");
      addButton.addClickHandler(new ClickHandler()
      {
        public void onClick(ClickEvent event)
        {
          addNote();
        }
      });
      menu.add(addButton);

      Button btnNewButton = new Button("Mes T\u00E2ches");
      btnNewButton.addClickHandler(new ClickHandler()
      {
        public void onClick(ClickEvent event)
        {
          clientFactory.getGeneralView().getPresenter().goTo(new GeneralViewPlace("Accueil"));
        }
      });
      menu.add(btnNewButton);
    }

    HTMLPanel content = new HTMLPanel("");
    content.getElement().setId("content");
    rootPanel.add(content);

    EventBus eventBus = clientFactory.getEventBus();
    PlaceController placeController = clientFactory.getPlaceController();

    // Start ActivityManager for the main widget with our ActivityMapper
    ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
    ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
    activityManager.setDisplay(appWidget);

    // Start PlaceHistoryHandler with our PlaceHistoryMapper
    AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
    PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
    historyHandler.register(placeController, eventBus, defaultPlace);
    content.add(appWidget);

    // Goes to the place represented on URL else default place
    historyHandler.handleCurrentHistory();

  }

  protected void addNote()
  {
    clientFactory.getGeneralView().getPresenter().goTo(new AddTaskViewPlace("Add"));
  }

  public boolean isMobile()
  {
    if (clientFactory.getClass().toString().contains("Mob"))
      return true;
    else
      return false;
  }

  public static native String getUA()
  /*-{
    return $wnd.navigator.userAgent;
  }-*/;
}
